﻿using System;
using System.Globalization;
using Pantry.Business;
using Pantry.Entity;

namespace Pantry.Web.Users
{
    public partial class ProgramDetail : System.Web.UI.Page
    {
        //private string action = "INSERT";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                var programBus = new ProgramBus();
                var programId = Convert.ToInt32(Request.QueryString.Get("Id"),CultureInfo.CurrentCulture);
                var program = (Program)programBus.RetrieveEntity(programId);
                ButtonDeactivate.Enabled = false;
                if (program!=null)
                {
                    TextBoxProgramId.Text = program.ProgramId.ToString(CultureInfo.InvariantCulture);
                    TextBoxProgramName.Text = program.ProgramName;
                    TextBoxDescription.Text = program.Description;
                    TextBoxIsActive.Text = program.IsActive.ToString(CultureInfo.InvariantCulture);
                    HiddenContactId.Value = program.ContactId.ToString(CultureInfo.InvariantCulture);
                    TextBoxContact.Text = program.ContactName.ToString(CultureInfo.InvariantCulture);
                    ButtonSave.Text = "Save";
                    ViewState["action"] = "UPDATE";
                    if (program.IsActive)
                    {
                        ButtonDeactivate.Enabled = true;
                    }
                }
                else
                {
                    ButtonSave.Text = "Insert";
                    ButtonDeactivate.Visible=false;
                    ViewState["action"] = "INSERT";
                }

                var contactBus=new ContactBus();
                RepeaterContacts.DataSource = contactBus.ListActivatedContact();
                RepeaterContacts.DataBind();
            }
        }

        protected void ButtonSaveClick(object sender, EventArgs e)
        {
            var program = new Program
            {
                ProgramName = TextBoxProgramName.Text,
                Description = TextBoxDescription.Text,
                ContactId = Convert.ToInt32(HiddenContactId.Value,CultureInfo.InvariantCulture),
                IsActive = true,
                OrganizationId = 2
            };

            var programBus = new ProgramBus();
            var thisAction = ViewState["action"].ToString();
            int affected;
            if (thisAction=="INSERT")
            {
                affected = programBus.Insert(program);
                if (affected==-2)
                {
                    LabelNotify.Text = @"<script>alert('Programe is exits');</script>";
                }
                else if (affected>0)
                {
                    Response.Redirect("ViewListProgram.aspx");
                }
            }
            else
            {
                program.ProgramId = Convert.ToInt32(TextBoxProgramId.Text,CultureInfo.CurrentCulture);
                affected = programBus.Update(program);
                if (affected == -2)
                {
                    LabelNotify.Text = @"<script>alert('Programe is exits');</script>";
                }
                else if (affected > 0)
                {
                    Response.Redirect("ViewListProgram.aspx");
                }
            }
        }

        protected void ButtonDeactivateClick(object sender, EventArgs e)
        {
            var thisAction = ViewState["action"].ToString();
            if (thisAction == "INSERT")
            {
                return;
            }
            var program = new Program
            {
                IsActive = Convert.ToBoolean(TextBoxIsActive.Text,CultureInfo.InvariantCulture),
                ProgramId = Convert.ToInt32(TextBoxProgramId.Text,CultureInfo.InvariantCulture)
            };

            var programBus = new ProgramBus();
            programBus.ChangeIsActive("Program","IsActive","False","ProgramId",program.ProgramId);
            var path = System.Web.HttpContext.Current.Request.Url.AbsolutePath;
            path += "?Id="+program.ProgramId;
            Response.Redirect(path);
        }
    }
}
